/**
 * 封装Axios
 * 处理请求、响应错误信息
 */
 import { Message } from "element-ui"; //引用饿了么UI消息组件
 import axios from "axios"; //引用axios
 import Cookies from "js-cookie";
 import { getToken } from '@/utils/auth' 
 
 axios.defaults.headers.post["Content-Type"] =
   "application/x-www-form-urlencoded";
 
 // create an axios instance
 const service = axios.create({
   baseURL: "/", // 所有异步请求都加上/api,nginx转发到后端Springboot
   withCredentials: true, // send cookies when cross-domain requests
   timeout: 5000 // request timeout
 });
 
 // request interceptor
 service.interceptors.request.use(
   config => {
    //  config.headers["Token"] = Cookies.get("USER_TOKEN") || "";
    //  config.headers['rolePlay'] = 'userApp';
     config.headers['Authorization'] =  getToken() || ""
     return config;
   },
   err => {
     return Promise.reject(err);
   }
 );
 // response interceptor
 service.interceptors.response.use(
   /**
    * If you want to get http information such as headers or status
    * Please return  response => response
    */
 
   /**
    * Determine the request status by custom code
    * Here is just an example
    * You can also judge the status by HTTP Status Code
    */
 
   response => {
     const res = response.data; //res is my own data
     if (res.code === 99999) {
       Cookies.set("USER_TOKEN", "");
       sessionStorage.setItem("userInfo", "");
       location.href = "/";
     }
     return res;
   },
   error => {
     console.log("err" + error); // for debug
     Message({
       message: "连接超时",
       type: "error",
       duration: 5 * 1000
     });
     return Promise.reject(error);
   }
 );
 
 export default service; //导出封装后的axios
 